/*
 * generator.h
 * Code generator for uPL0c.
 *
 * Copyright 2024 dh33ex <dh33ex@riseup.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA or visit <http://www.gnu.org/licenses/>.
 *
 *
 */

#ifndef GENERATOR_H
#define GENERATOR_H

/* value in words! */
#define MEMORY_SIZE             (0x400/4) /* for code */
#define PROCEDURE_START_ADDR    (0x200/4)

#define  MOV    0
#define  AND    1
#define  OR     2
#define  XOR    3
#define  NOT    4
#define  ADD    5
#define  SUB    6
#define  MUL    7
#define  DIV    8

#define  LSL    9
#define  ASR   10
#define  ROT   11

#define LDW     0
#define LDB     1
#define STW     2
#define STB     3

#define JMP     0
#define JMI     1
#define JEQ     2
#define JCS     3
#define JVS     4
#define JPL     5
#define JNE     6
#define JCC     7
#define JVC     8

#define V_FLAG  1
#define U_FLAG  2

extern unsigned int RH;    /* register pointer */
extern unsigned int SP;    /* stack pointer */
extern unsigned int SB;    /* static base */
extern unsigned int LNK;   /* link */

extern unsigned int PC;
extern unsigned int code[MEMORY_SIZE];
extern unsigned int PROC;

/* global functions */
void put_f0(unsigned int op, unsigned int reg1, unsigned int reg2, unsigned int reg3);
void put_f1(unsigned int op, unsigned int reg1, unsigned int reg2, unsigned int im);
void put_f2(unsigned int op, unsigned int reg1, unsigned int reg2, unsigned int off);
int put_f3(unsigned int op, unsigned int cond, unsigned int off);

void write_code(void);

#endif
